/*
 * @(#)ArchTreeSelectionListener.java   10/01/01
 * 
 * Copyright (C) 2009 Ecole des Mines d'Ales
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: yulinz88@gmail.com
 */



/**
 *
 */
package fr.ema.arch3d.editor.mainGUI.listeners;

import fr.ema.arch3d.editor.mainGUI.TreeTable.JTreeTable;
import fr.ema.arch3d.editor.mainGUI.archExplorer.ArchTree;

import org.jgraph.JGraph;

import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JViewport;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

//~--- classes ----------------------------------------------------------------

/**
 * class description
 *
 * @version    version 18 sept. 2009
 * @author     Huaxi (Yulin) ZHANG
 */

/**
 * Class description
 *
 *
 * @version        1.0, 09/09/13
 * @author         Huaxi (Yulin) ZHANG
 */
public class ArchTreeSelectionListener implements TreeSelectionListener {

    /** Field description */
    static ArchTree archTree;

    /** Field description */
    static JTabbedPane editorTabbedPane;

    /** Field description */
    static JScrollPane graphPane;

    /** Field description */
    static JGraph graphS, graphC, graphA;

    /** Field description */
    static JTreeTable specTreeTable, configTreeTable, assTreeTable;

    /**
     * Constructs...
     *
     * @param archTree
     * @param editorTabbedPane  
     * @param graphS
     * @param graphC
     * @param graphA
     * @param specTreeTable  
     * @param assTreeTable
     * @param configTreeTable
     */
    public ArchTreeSelectionListener(ArchTree archTree, JTabbedPane editorTabbedPane, JGraph graphS, JGraph graphC,
                                     JGraph graphA, JTreeTable specTreeTable, JTreeTable configTreeTable,
                                     JTreeTable assTreeTable) {
        this.archTree         = archTree;
        this.editorTabbedPane = editorTabbedPane;
        this.graphPane        = (JScrollPane) (editorTabbedPane.getComponent(0));
        this.graphA           = graphA;
        this.graphC           = graphC;
        this.graphS           = graphS;
        this.specTreeTable    = specTreeTable;
        this.configTreeTable  = configTreeTable;
        this.assTreeTable     = assTreeTable;
    }

    /**
     * Method description
     *
     * @param arg0  
     */
    public void valueChanged(TreeSelectionEvent arg0) {
        editorTabbedPane.setSelectedIndex(0);

        // find select node in the tree
        TreePath path = archTree.getSelectionPath();

        if (path == null) {
            return;
        }

        DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) path.getLastPathComponent();

        // define three structure nodes
        DefaultMutableTreeNode specNode   = new DefaultMutableTreeNode();
        DefaultMutableTreeNode configNode = new DefaultMutableTreeNode();
        DefaultMutableTreeNode assNode    = new DefaultMutableTreeNode();

        // find these nodes in the tree
        for (int i = 0; i < archTree.getModel().getChildCount(archTree.getModel().getRoot()); i++) {
            DefaultMutableTreeNode tn =
                (DefaultMutableTreeNode) archTree.getModel().getChild(archTree.getModel().getRoot(), i);

            if (tn.getUserObject().equals("Specification")) {
                specNode = tn;
            } else if (tn.getUserObject().equals("Configuration")) {
                configNode = tn;
            } else if (tn.getUserObject().equals("Assembly")) {
                assNode = tn;
            }
        }

        if (selectedNode.isNodeAncestor(specNode)) {

            // active the correspondent graph cell to selection
            JViewport jvp = (JViewport) graphPane.getComponent(0);

            jvp.setView(graphS);

            // set synchronize selection with graph
            if (selectedNode.isLeaf()) {
                for (int i = 0; i < graphS.getModel().getRootCount(); i++) {
                    if (graphS.getModel().getRootAt(i).toString() != null) {
                        if (graphS.getModel().getRootAt(i).toString().startsWith(
                                (String) selectedNode.getUserObject())) {
                            graphS.setSelectionCell(graphS.getModel().getRootAt(i));
                        }
                    }
                }
            }

            graphS.addGraphSelectionListener(new SyncGraphSelectionListener(archTree, graphPane, graphS, graphC,
                    graphA));

            // active the correspondent treeTable
        }

        // active the correspondent graph cell to selection
        else if (selectedNode.isNodeAncestor(configNode)) {
            JViewport jvp = (JViewport) graphPane.getComponent(0);

            jvp.setView(graphC);

            // set synchronize selection with graph
            if (selectedNode.isLeaf()) {
                for (int i = 0; i < graphC.getModel().getRootCount(); i++) {
                    if (graphC.getModel().getRootAt(i).toString() != null) {
                        if (graphC.getModel().getRootAt(i).toString().startsWith(
                                (String) selectedNode.getUserObject())) {
                            graphC.setSelectionCell(graphC.getModel().getRootAt(i));
                        }
                    }
                }
            }

            graphC.addGraphSelectionListener(new SyncGraphSelectionListener(archTree, graphPane, graphS, graphC,
                    graphA));
        }

        // active the correspondent graph cell to selection
        else if (selectedNode.isNodeAncestor(assNode)) {
            JViewport jvp = (JViewport) graphPane.getComponent(0);

            jvp.setView(graphA);

            // set synchronize selection with graph
            if (selectedNode.isLeaf()) {
                for (int i = 0; i < graphA.getModel().getRootCount(); i++) {
                    if (graphA.getModel().getRootAt(i).toString() != null) {
                        if (graphA.getModel().getRootAt(i).toString().startsWith(
                                (String) selectedNode.getUserObject())) {
                            graphA.setSelectionCell(graphA.getModel().getRootAt(i));
                        }
                    }
                }
            }

            graphA.addGraphSelectionListener(new SyncGraphSelectionListener(archTree, graphPane, graphS, graphC,
                    graphA));
        }

        // define the graph selection listener
    }
}
